package Interfaz;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import Logica.*;
import javax.imageio.ImageIO;
import javax.swing.*;


@SuppressWarnings("serial")
public class GUIBoard extends JPanel { 


	protected GUIRobot rob;
	protected final int OFFSET = 0;
	protected final int SPACE = 40;
	protected final int SPACEY = 40;
	@SuppressWarnings("rawtypes")
	protected ArrayList GUIArenas = new ArrayList();
	@SuppressWarnings("rawtypes")
	protected ArrayList GUIAsfaltos = new ArrayList();
	@SuppressWarnings("rawtypes")
	protected ArrayList GUIConcretos = new ArrayList();
	@SuppressWarnings("rawtypes")
	protected ArrayList GUIFuegos = new ArrayList();
	@SuppressWarnings("rawtypes")
	protected ArrayList GUIAguas = new ArrayList();
	@SuppressWarnings("rawtypes")
	protected ArrayList GUICajas = new ArrayList();
	@SuppressWarnings("rawtypes")
	protected ArrayList GUIParedes = new ArrayList();
	@SuppressWarnings("rawtypes")
	protected ArrayList GUIPlantas = new ArrayList();
	@SuppressWarnings("rawtypes")
	protected ArrayList GUIIniFin = new ArrayList();
	protected int finX, finY;
	protected Mapa map;
	protected Juego j;
	protected int w = 0;
	protected int h = 0;
	protected File fi= new File("mapa.txt");
	protected ReadFile rf=new ReadFile();
	protected String level = rf.leer("mapa.txt");



	public GUIBoard(Juego ju) {
		j = ju;
		map=j.getMapa();
		KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
		manager.addKeyEventDispatcher(new MyDispatcher());
		setFocusable(true);
		initWorld();
		Civil robot = new Civil(map.getInicio()[0],map.getInicio()[1],map);
		j.setRobot(robot);
		rob = new GUIRobot((map.getInicio()[0]*40)+10,(map.getInicio()[1]*40)+10);
	}

	public int getBoardWidth() {
		return this.w;
	}

	public int getBoardHeight() {
		return this.h;
	}

	@SuppressWarnings("unchecked")
	public final void initWorld() {

		int x = OFFSET;
		int y = OFFSET;
		//(0,0) empieza desde arriba a la izquierda
		int i=0;
		int j=0;
		GUIArena s;
		GUIAsfalto b;
		GUIConcreto a;
		GUIFuego f;
		GUIAgua w;
		GUICaja c;
		GUIPared p;
		GUIPlanta pl;
		GUIInicio ini;
		GUILlegada fin;
		for (int n = 0; n < level.length(); n++) {

			char item = level.charAt(n);
			if (item == '\n') {
				y += SPACEY;
				i=0;
				if (this.w < x) {
					this.w = x;
				}
				j++;
				x = OFFSET;
			} 
			else if (item == '&') {
				f = new GUIFuego(x, y);
				GUIFuegos.add(f);
				map.cambiarTerreno(i, j, '&');
				x += SPACE;
				i++;
			} else if (item == '@') {
				w = new GUIAgua(x, y);
				GUIAguas.add(w);
				map.cambiarTerreno(i, j, '@');
				x += SPACE;
				i++;
			} else if (item == 'S') {
				s = new GUIArena(x, y);
				GUIArenas.add(s);
				map.cambiarTerreno(i, j, '#');
				map.setInicio(i, j);
				ini=new GUIInicio(x,y);
				GUIIniFin.add(ini);
				x += SPACE;
				i++;
			} else if (item == 'F') {
				s = new GUIArena(x, y);
				finX=x;
				finY=y;
				GUIArenas.add(s);
				map.cambiarTerreno(i, j, '#');
				map.setFin(i, j);
				fin=new GUILlegada(x,y);
				GUIIniFin.add(fin);
				x += SPACE;
				i++;
			} else if (item == '#') {
				s = new GUIArena(x, y);
				GUIArenas.add(s);
				map.cambiarTerreno(i, j, '#');
				x += SPACE;
				i++;
			} else if (item == '_') {
				b = new GUIAsfalto(x, y);
				GUIAsfaltos.add(b);
				map.cambiarTerreno(i, j, '_');
				x += SPACE;
				i++;
			} else if (item == 'X') {
				a = new GUIConcreto(x,y);
				GUIConcretos.add(a);
				Concreto caj = new Concreto (i,j,map);
				map.agregarObjetoNoDestructible(i, j, caj);
				x += SPACE;
				i++;

			} else if (item == ' ') {
				x += SPACE;
				i++;
			}else if (item == '['){
				c = new GUICaja(x,y);
				GUICajas.add(c);
				Caja caj = new Caja (i,j,map);
				map.agregarObjetoDestructible(i, j, caj);
				x += SPACE;
				i++;
			}else if (item == '{'){
				pl = new GUIPlanta(x,y);
				GUIPlantas.add(pl);
				Planta caj = new Planta (i,j,map);
				map.agregarObjetoDestructible(i, j, caj);
				x += SPACE;
				i++;
			}else if (item == '|'){
				p = new GUIPared(x,y);
				GUIParedes.add(p);
				Pared par = new Pared (i,j,map);
				map.agregarObjetoDestructible(i, j, par);
				x += SPACE;
				i++;
			}
			h = y;
		}
	}


	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void buildWorld(Graphics g) {

		Image img=null;
		try {
			img = ImageIO.read(new File("background.JPG"));
		} catch (IOException e) {}
		g.drawImage(img, 0, 0, this);

		ArrayList world = new ArrayList();
		world.addAll(GUIAguas);
		world.addAll(GUIFuegos);
		world.addAll(GUIArenas);
		world.addAll(GUIConcretos);
		world.addAll(GUIAsfaltos);
		world.addAll(GUICajas);
		world.addAll(GUIParedes);
		world.addAll(GUIPlantas);
		world.addAll(GUIIniFin);

		for (int i = 0; i < world.size(); i++) {

			Actor item = (Actor) world.get(i);
			g.drawImage(item.getImage(), item.x(), item.y(), this);

		}
		g.drawImage(rob.getImage(),rob.x(), rob.y(), this);
	}


	@Override
	public void paint(Graphics g) {
		super.paint(g);
		buildWorld(g);
	}




	private class MyDispatcher implements KeyEventDispatcher {
		@SuppressWarnings("static-access")
		public boolean dispatchKeyEvent(KeyEvent e) {

			int mov;
			int key = e.getKeyCode();
			key = e.getKeyCode(); 

			if(e.getID() == e.KEY_PRESSED){

				if (key == KeyEvent.VK_LEFT) {
					mov=j.moverRobot("izquierda");
					if(mov==1){
						rob.move(-SPACE,0);
						rob.setImage(new ImageIcon("robotIzquierda.png").getImage());
					}else
						if (mov==2){
							rob.move(-SPACE,0);
							JOptionPane.showMessageDialog(getParent(), "Ganaste!");
							Civil robot = new Civil(map.getInicio()[0],map.getInicio()[1],map);
							j.setRobot(robot);
							rob = new GUIRobot((map.getInicio()[0]*40)+10,(map.getInicio()[1]*40)+10);
						}
				}else if (key == KeyEvent.VK_RIGHT) { 
					mov=j.moverRobot("derecha");
					if(mov==1){
						rob.move(SPACE,0);
						rob.setImage(new ImageIcon("robotDerecha.png").getImage());
					}else
						if (mov==2){
							rob.move(SPACE,0);

							JOptionPane.showMessageDialog(getParent(), "Ganaste!");
							Civil robot = new Civil(map.getInicio()[0],map.getInicio()[1],map);
							j.setRobot(robot);
							rob = new GUIRobot((map.getInicio()[0]*40)+10,(map.getInicio()[1]*40)+10);
						}
				}else if (key == KeyEvent.VK_UP) {
					mov=j.moverRobot("arriba");
					if(mov==1){
						rob.move(0,-SPACE); 
						rob.setImage(new ImageIcon("robotArriba.png").getImage());
					}else
						if (mov==2){
							rob.move(0,-SPACE); 
							JOptionPane.showMessageDialog(getParent(), "Ganaste!");
							Civil robot = new Civil(map.getInicio()[0],map.getInicio()[1],map);
							j.setRobot(robot);
							rob = new GUIRobot((map.getInicio()[0]*40)+10,(map.getInicio()[1]*40)+10);
						}
				}else if (key == KeyEvent.VK_DOWN){ 
					mov=j.moverRobot("abajo");
					if(mov==1){
						rob.move(0,SPACE);
						rob.setImage(new ImageIcon("robotAbajo.png").getImage());
					}else
						if (mov==2){
							rob.move(0,SPACE);
							JOptionPane.showMessageDialog(getParent(), "Ganaste!");
							Civil robot = new Civil(map.getInicio()[0],map.getInicio()[1],map);
							j.setRobot(robot);
							rob = new GUIRobot((map.getInicio()[0]*40)+10,(map.getInicio()[1]*40)+10);
						}
				}

			}
			repaint(); 
			return false;
		}
	}


}